﻿using System;
using System.Linq;

namespace XEvolve.Core.Runtime.Attributes
{
    [AttributeUsage(AttributeTargets.Parameter)]
    public class RequiredAttribute : Attribute, IDependencyAttribute
    {
        private readonly object[] identifiers;

        public RequiredAttribute(params object[] identifiers)
        {
            this.identifiers = identifiers;
        }

        public object ProvideValue(IExtensionRegistry registry)
        {
            var plugins = registry.GetExtensionPoint("xevolve.plugins");
            var values = identifiers.Select(id => plugins.GetExtension(id)).ToArray();
            if (values.Any(value => value == null))
            {
                throw new UnableToInstallPluginException("Some required plugins can not be found!");
            }
            return values;
        }
    }
}